<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/18
 * Time: 16:26
 */

namespace core_class;

use Illuminate\Database\Capsule\Manager as Capsule;
use GuzzleHttp\Client;
use phpQuery;

class Gather
{
    /**
     * 获取楼盘信息
     */
    public function gather_loupan(){
        $url = $this->loupan_base_url.$this->cur_page;
        $file_contents = Cache::cache_file_get_contents($url);
        $new_str = iconv( "gbk","utf-8", $file_contents);
        phpQuery::newDocument($new_str);
        foreach (pq('.sf_xf_prodiv') as $index => $area){
            $tel_area = pq('.sf_xf_tel', $area);
            $div = pq('.sf_xf_projj', $area);

            $text = pq($div)->text();
            preg_match_all("/^.*热销户型：(.*)?项目地址.*$/", $text, $rxhx_arr);
            preg_match_all("/^.*项目地址：(.*)?\[.*$/", $text, $xmdz_arr);
            $rxhx = $rxhx_arr[1][0];
            $xmdz = $xmdz_arr[1][0];
            $jg = pq('font', $tel_area)->text();
            $dh_str = pq($tel_area)->text();
            $dh = str_replace($jg, "", $dh_str);
            $data = [
                "楼盘" => pq('a:eq(0)', $div)->text(),
                "楼盘类型" => pq('font:eq(0)>a', $div)->attr('title'),
                "开发企业" => pq('a.ztys2', $div)->text(), //开发企业
                "电话" => $dh,
                "参考价格" => $jg,
                "项目坐落" => $xmdz,
                "热销户型" => $rxhx,
                "备注" => $text,
            ];
            $this->insert_data($data);
        }
        if($this->cur_page < $this->max_page){
            $this->cur_page++;
            $this->gather_loupan();
        }
    }

    function insert_data( $loupan){
        date_default_timezone_set("Asia/Shanghai");
        try{
            echo json_encode($loupan, JSON_UNESCAPED_UNICODE);
            Capsule::table($this->table_name)->insert([
                "楼盘" => $loupan["楼盘"],
                "楼盘类型" => $loupan["楼盘类型"],
                "开发企业" => $loupan["开发企业"],
                "电话" => $loupan["电话"],
                "参考价格" => $loupan["参考价格"],
                "项目坐落" => $loupan["项目坐落"],
                "热销户型" => $loupan["热销户型"],
                "备注" => $loupan["备注"],
            ]);
        }catch (Exception $e){
            var_dump($e);
        }
    }


    private $kaishi_time;
    private $cur_page;
    private $max_page;
    private $loupan_hanghao = 0;
    private $zhuang_hanghao = 0;
    private $loupan_base_url = 'http://www.fang99.com/buycenter/buildingsearch.aspx?property=10&page=';
    private $table_name = 'loupan_fang99_oop';
    private function __construct($qi_page, $max_page)
    {
        $this->cur_page = $qi_page;
        $this->max_page = $max_page;
        $this->kaishi_time = time();
    }

    private function toLog(){
        echo "\n\n开始时间：".date('Ymd H:i:s', $this->kaishi_time)."->".date('Ymd H:i:s', time())." 已用时长：".((time()-$this->kaishi_time)/60).'m';
        echo "\n楼盘页码：{$this->cur_page}/{$this->max_page} 楼盘页行号：{$this->loupan_hanghao} 幢页行号(楼层)：{$this->zhuang_hanghao} 房间位置：{$this->fangjian_index}";
    }

    public static function run($qi_page, $max_page){
        $gather = new self($qi_page, $max_page);
        $gather->gather_loupan();
    }
}